<template>
   <div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
    <el-breadcrumb-item :to="{ path: '/' }" style="font-size: 35px">个人信息</el-breadcrumb-item>
  </el-breadcrumb>

  <el-divider></el-divider>

     <el-form label-width="80px">
       <el-form-item label="头像">
         <img :src="loginInfo.avatar" style="float:left;width: 145px;height: 145px;border-radius: 10px">
         <!--上传组件开始-->
         <el-upload
             action="http://localhost:29112/upload/image/client"
             name="file"
             :limit=1
             list-type="picture-card"
             :on-preview="handlePictureCardPreview"
             :on-success="handleSuccess"
             :on-remove="handleRemove">
           <i class="el-icon-plus"></i>
         </el-upload>
         <el-dialog :visible.sync="dialogVisible">
           <img width="100%" :src="imageUrl" alt="">
         </el-dialog>
         <!--上传组件结束-->
       </el-form-item>

       <el-form-item label="用户名">
         <el-input style="width: 200px;float:left;" v-model="loginInfo.username" disabled></el-input>
       </el-form-item>
       <el-form-item label="手机号" :error="hasShownError ? '手机号必须为11位数字' : ''">
         <el-input
             style="width: 200px; float: left;"
             v-model="loginInfo.phone"
             placeholder="请输入手机号"
             :maxlength="11"
             pattern="^\d{11}$"
             title="手机号必须为11位数字"
             @input="handlePhoneInput"
         ></el-input>
       </el-form-item>
       <el-form-item label="家庭住址">
         <el-input style="width: 200px;float:left;" v-model="loginInfo.homeAddress" ></el-input>
       </el-form-item>
       <el-form-item>
         <el-button type="primary" @click="save()" style="margin-left: -450px">保存信息</el-button>
       </el-form-item>
     </el-form>
   </div>
</template>

<script>
import axios from "axios";
import TokenAxios from "@/http/TokenAxios";
import BaseUrl from "@/http/BaseUrl";

export default {
  methods: {
    handlePhoneInput() {
      const phone = this.loginInfo.phone;
      if (phone && !/^\d{11}$/.test(phone) && !this.hasShownError) {
        this.hasShownError = true;
      }else {
        this.hasShownError = false;
      }
    },
    handleRemove(file, fileList) {
      this.imageUrl="";
      console.log(file,fileList);
      let url='http://localhost:19780/upload/image/user-avatar/remove?url='+file.response.data;
      TokenAxios.post(url).then((response)=>{
        if (response.data.state===20000){
          this.$message.success("服务器文件删除成功");
        }
      })
    },
    handlePictureCardPreview(file) {
      debugger
      this.imageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(response,file,fileList){
      debugger
      console.log(response.data)
      this.imageUrl=response.data.url;
      console.log(this.imageUrl)
    },
    save() {
      //判断是否选择了新的头像
      if (this.imageUrl!=""){
        this.loginInfo.avatar = this.imageUrl;
      }
      //发出修改请求
      debugger
      console.log(this.imageUrl)
      this.param.picImgeRul=this.loginInfo.avatar;

      debugger
      TokenAxios.post(BaseUrl.INFORMATIOON+'/employees/avatar',this.param).then((response)=>{
        debugger
        if (response.data.state===20000){
          debugger
          response.data.picImgeRul=this.imageUrl;
          this.$message.success("修改成功!");
          //把修改完用户信息的数据 重新赋值给localStorage.user
          localStorage.loginInfo = JSON.stringify(this.loginInfo);
          debugger
        }
      })

      TokenAxios.post(BaseUrl.INFORMATIOON+'/employees/update',this.loginInfo).then((response) => {
        debugger
        let json=response.data
        if(json.state==20000){
          this.$message.success("修改成功!");
          //把修改完用户信息的数据 重新赋值给localStorage.user
          localStorage.loginInfo = JSON.stringify(this.loginInfo);
          /* localStorage.setItem('loginInfo',JSON.stringify(this.loginInfo))*/
        }
      })
    },
  },

  data() {
    return {
      hasShownError: false,
      dialogImageUrl: '',
      dialogVisible: false,
      loginInfo: localStorage.loginInfo ? JSON.parse(localStorage.loginInfo) : null,
      // loginInfo:JSON.parse(localStorage.getItem('loginInfo')) || {},
      imageUrl: '',
      param: {picImgeRul:''}
    }
  }
}
</script>
